package org.springblade.modules.admin.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.aliyun.dyvmsapi20170525.models.SingleCallByTtsResponseBody;
import com.sida.voice.service.VoiceClient;
import com.sida.voice.service.VoiceTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.sms.model.SmsResponse;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.modules.admin.service.WeChatDialogueService;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 异步工具类
 *
 * @author zcc
 * @version 1.0
 * @date 2023/3/8 9:49
 */
@Component
@Slf4j
public class AsyncUtils {

	@Autowired
	private WeChatDialogueService weChatDialogueService;

	@Autowired
	private VoiceClient voiceClient;
	@Autowired
	private Environment environment;

	/**
	 * 发送短信模板
	 *
	 * @param openId          微信的open
	 * @param templateId      模板id
	 * @param templateDataMap 消息内容
	 */
	@Async("customExecutor")
	public void sendWeiXinTemplate(String openId, String templateId, Map<String, String> templateDataMap) {
		Boolean aBoolean = weChatDialogueService.sendMsg(openId, templateId, templateDataMap);
		log.info("===发送微信消息模板状态=" + aBoolean);
	}


	/**
	 * 发送短信
	 *
	 * @param code   短信模板code
	 * @param params 参数
	 * @param phone  电话
	 */
	@Async("customExecutor")
	public void sendSms(String code, Map<String, String> params, String phone) {
		final SmsResponse smsResponse = SmsUtil.sendMessage(code, BladeConstant.ADMIN_TENANT_ID, params, phone);
		log.info("===发送短信消息结果=" + JSON.toJSONString(smsResponse));
	}


	/**
	 * 发送语音播报
	 *
	 * @param voiceTemplate 消息体
	 */
	@Async("customExecutor")
	public void sendPhone(VoiceTemplate voiceTemplate) {
		String evn = environment.getActiveProfiles()[0];
		if (evn.equals(AppConstant.TEST_CODE)) {
			voiceTemplate.setPhoneNumber("13046341037");
		}
		final SingleCallByTtsResponseBody responseBody = voiceClient.send(voiceTemplate);
		log.info("===发送语音播报消息结果=" + JSON.toJSONString(responseBody));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   POST 请求
	 * }
	 * @Date 9:16 2023/4/18
	 * @Param url 请求地址
	 * @Param body 请求参数
	 * @return 请求结果
	 **/
	@Async("customExecutor")
	public void createPost(String url,String body){
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}","POST", url, body);
		HttpUtil.createPost(url).body(body).execute().body();
	}

	/**
	 *  POST请求 (携带token)
	 * @param url
	 * @param body
	 * @param request
	 */
	@Async("customExecutor")
	public void createPost(String url, String body, HttpServletRequest request){
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}, 请求token:{}","POST", url, body,  AuthUtil.getHeader(request));
		HttpUtil.createPost(url).header(TokenConstant.HEADER, AuthUtil.getHeader(request)).body(body).execute().body();
	}


}
